import { Button } from 'antd-mobile';

const PromiseErrorDemo = () => {
	// Unhandled promise rejection
	const triggerUnhandledError = () => {
		new Promise((_, reject) => {
			reject(new Error('Network timeout (unhandled)'));
		});
	};

	// Handled promise rejection
	const triggerHandledError = () => {
		const promise = new Promise((_, reject) => {
			reject(new Error('Network timeout (handled)'));
		});

		promise.catch(error => {
			console.log('已经处理', error);
		});
	};

	// Rejection without Error object (won't have stack trace)
	const triggerRejectWithoutError = () => {
		new Promise((_, reject) => {
			reject('Network timeout (no stack)');
		});
	};

	return (
		<div
			style={{ padding: 16, display: 'flex', flexDirection: 'column', gap: 12 }}
		>
			<Button color='primary' size='middle' onClick={triggerUnhandledError}>
				错误未处理
			</Button>

			<Button color='primary' size='middle' onClick={triggerHandledError}>
				错误已处理
			</Button>

			<Button color='primary' size='middle' onClick={triggerRejectWithoutError}>
				reject不产生堆栈，不上报
			</Button>
		</div>
	);
};

export default PromiseErrorDemo;
